<template>
	<view class="yajinrecord" :style="{'height':show?'100vh':''}">
		<headertop title="商城订单" str="orderlist" str1="oridei" :back1="back1"></headertop>
		<view class="yajinrecordback">

		</view>
		<view class="seachbox">
			<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/Frame@2x (1).png" class="lefticon" mode="">
			</image>
			<input type="text" v-model="query.keywords" placeholder="请输入关键词进行搜索" />
			<view class="seachboxbtn" @click="init('搜索')">
				搜索
			</view>
			<image src="/static/Frame@2x(15).png" class="closeicon" @click="closeinput" v-if="query.keywords" mode="">
			</image>
		</view>
		<scroll-view :scroll-left="leftnumstr" scroll-x="true" class="classifybox">
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.status==0?'itembox activecolor':'itembox'" @click="qiehuan(0)">
					<span>全部</span>
					<view class="activebox" v-if="query.status==0">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.status==1?'itembox activecolor':'itembox'" @click="qiehuan(1)">
					<span>待支付</span>
					<view class="activebox" v-if="query.status==1">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.status==2?'itembox activecolor':'itembox'" @click="qiehuan(2)">
					<span>待核销</span>
					<view class="activebox" v-if="query.status==2">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.status==3?'itembox activecolor':'itembox'" @click="qiehuan(3)">
					<span>待发货</span>
					<view class="activebox" v-if="query.status==3">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.status==4?'itembox activecolor':'itembox'" @click="qiehuan(4)">
					<span>待收货</span>
					<view class="activebox" v-if="query.status==4">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.status==5?'itembox activecolor':'itembox'" @click="qiehuan(5)">
					<span>已取消</span>
					<view class="activebox" v-if="query.status==5">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;" @click="qiehuan(6)">
				<view :class="query.status==6?'itembox activecolor':'itembox'">
					<span>已完成</span>
					<view class="activebox" v-if="query.status==6">

					</view>
				</view>
			</view>
		</scroll-view>
		<view class="itemboxss">

			<view class="msgicon" v-if="!list.length">
				<image src="/static/微信图片_20250514232056.png" mode="widthFix"></image>
				<view class="msgtext">
					暂无订单
				</view>
			</view>

			<view class="itembox" v-for="(item,index) in list" :key="index">
				<view class="itembox_center" @click="gopage(item.id)">
					<view class="topline">
						<view class="dingdanbinhao">
							<image src="https://39gongli.oss-cn-beijing.aliyuncs.com/static/微信图片_20250429143332.png"
								class="dingdanbinhao_icon" mode=""></image>
							<span>订单编号：{{item['order_no']}}</span>
						</view>
						<view class="statustext">
							{{statustext(item.status)}}
						</view>
					</view>
					<view class="spinfo" v-if="item.goods.length" v-for="(ite,index) in item.goods" :key="index">
						<image :src="ite['image']" mode=""></image>
						<view class="text1">
							<view class="l1">
								<span>{{ite['title']}}</span>
							</view>
							<view class="l2">
								{{ite['detail']}}
							</view>
							<view class="yajinbox">
								<span><span
										style="font-weight: bold;font-size: 30rpx;color: #7EBC79;">￥{{ite['price']}}</span></span>
								<view class="tihuofs">
									提货方式：{{item.emstype==1?'快递':'自提'}}
								</view>
							</view>
						</view>
					</view>
					<view class="bottomline" v-if="statustext(item.status)=='待核销'">
						<view class="" style="display: flex;">
							<view class="borderbtn" @click.stop="quxiaodingdan(item)">
								取消订单
							</view>
							<view class="btn" style="margin-left: 28rpx;" @click.stop="open(item)">
								核销码:{{item.hxnum}}
							</view>
						</view>
					</view>
					<view class="bottomline" v-if="statustext(item.status)=='待发货'">
						<view class="btn" @click.stop="quxiaodingdan(item)">
							取消订单
						</view>
					</view>
					<view class="bottomline" v-if="statustext(item.status)=='待收货'">
						<view class="btn" @click="qdsh1(item)">
							确认收货
						</view>
					</view>
					<view class="bottomline" v-if="statustext(item.status)=='待支付'">
						<view class="btn" @click.stop="ljzf(item)">
							立即支付
						</view>
					</view>
					<view class="" style="height: 28rpx;"
						v-if="statustext(item.status)!='待核销'&&statustext(item.status)!='待发货'&&statustext(item.status)!='待收货'&&statustext(item.status)!='待支付'">
					</view>
				</view>
			</view>
		</view>
		<up-popup :show="show" mode="center">
			<view class="popucenter">
				<image src="/static/Frame@2x(6).png" class="close" @click="show=false" mode="">
				</image>
				<view class="title">
					核销码
				</view>
				<canvas id="qrcode" canvas-id="qrcode"
					style="width: 332rpx;height: 332rpx;margin: 30rpx auto;"></canvas>
				<view class="btnss" @click="goResult(qrobj.hxnum)">
					核销码：{{qrobj.hxnum}}
				</view>
			</view>
		</up-popup>
	</view>
</template>
<script setup>
	import headertop from '@/components/header.vue'
	import UQRCode from '@/uni_modules/Sansnn-uQRCode/js_sdk/uqrcode/uqrcode.js';
	import {
		onPageScroll,
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import {
		orderlist,
		payorder,
		changstatus
	} from '@/api/commodity.js'
	import {
		ref,
		reactive
	} from 'vue'
	const back1 = ref('')
	const leftnumstr = ref('')
	const lastpage = ref(0)
	const qrobj = ref({})
	const list = ref([])
	const show = ref(false)
	const query = reactive({
		page: 1,
		limit: 10,
		keywords: '',
		status: 0
	})
	onPageScroll((e) => {
		console.log(e, 'eeeeeeeeeeeeeee');
		if (e.scrollTop > 100) {
			back1.value = '#84BD78'
		} else {
			back1.value = ''
		}
		if (lastpage.value > query.page) {
			query.page++
			init()
		}
	})

	function closeinput() {
		query.keywords = ''
		query.status = 0
		query.page = 1
		list.value = []
		init()
	}

	function goResult(str) {
		uni.setClipboardData({
			data: str, // 需要复制的内容
			success: () => {
				// 成功提示框显示 1 秒钟
				uni.showToast({
					title: '复制成功',
					icon: 'success',
					duration: 1000
				});
			},
			fail: () => {
				// 失败提示框显示 1 秒钟
				uni.showToast({
					title: '复制失败，请重试',
					icon: 'none',
					duration: 1000
				});
			}
		});
	}

	function open(item) {

		// 获取uQRCode实例
		var qr = new UQRCode();
		// 设置二维码内容
		qr.data = item.hxnum
		// 设置二维码大小，必须与canvas设置的宽高一致
		qr.size = 166;
		// 调用制作二维码方法
		qr.make();
		// 获取canvas上下文
		var canvasContext = uni.createCanvasContext('qrcode', this); // 如果是组件，this必须传入
		// 设置uQRCode实例的canvas上下文
		qr.canvasContext = canvasContext;
		// 调用绘制方法将二维码图案绘制到canvas上
		qr.drawCanvas();
		show.value = true
		qrobj.value = item
	}
	onLoad((opt) => {
		if (opt.active == undefined) {
			query.status = 0
		} else {
			query.status = opt.active * 1 + 1
		}
		if (query.status > 4) {
			leftnumstr.value = '500rpx'
		} else {
			leftnumstr.value = '0rpx'
		}
	})
	onShow(() => {
		query.page = 1
		list.value = []
		init()
	})

	function quxiaodingdan(item) {
		uni.navigateTo({
			url: '/sub_my/my/quxiaoorder1?id=' + item.id
		})
	}

	function qiehuan(num) {
		console.log(num, 'num--------------')
		if (num != 0) {
			query.keywords = ''
		}
		query.status = num
		query.page = 1
		list.value = []
		init()
		// console.log(list.value);
	}

	function qdsh1(item) {
		uni.showModal({
			title: '提示', // 标题（可选）
			content: '确定要确认收货吗？', // 内容（可选）
			confirmText: '确定', // 确认按钮文字（可选，默认“确定”）
			cancelText: '取消', // 取消按钮文字（可选，默认“取消”）
			success: (res) => {
				if (res.confirm) {
					qdsh(item)
					// 执行确定后的逻辑
				} else if (res.cancel) {
					console.log('用户点击了取消');
					// 执行取消后的逻辑
				}
			},
			fail: (err) => {
				console.error('弹窗调用失败', err);
			}
		});
	}
	async function qdsh(item) {
		const res = await changstatus({
			id: item.id,
			status: 2
		})
		if (res.data.code == 1) {
			init()
		} else {
			uni.showToast({
				title: res.data.msg,
				icon: 'none'
			})
		}
	}
	async function ljzf(item) {
		const res = await payorder({
			id: item.id
		})
		if (res.code == 0) {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
			return
		}
		if (res.data.code == 1) {
			var payobj = res.data.data.pay
			uni.requestPayment({
				timeStamp: String(payobj.timestamp),
				nonceStr: payobj.noncestr,
				package: payobj.package,
				signType: 'MD5',
				paySign: payobj.sign,
				success(res) {
					query.page = 1
					list.value = []
					init()
				},
				fail(e) {
					setTimeout(() => {
						uni.showToast({
							title: '支付失败',
							icon: 'none'
						})
					}, 500)

				}
			})
		} else {
			uni.showToast({
				title: res.data.msg,
				icon: 'none'
			})
		}
	}

	function gopage(id) {
		uni.navigateTo({
			url: '/sub_my/my/shangchengdetail?id=' + id
		})
	}

	function statustext(num) {
		if (num == 0) {
			return '待支付'
		} else if (num == 1) {
			return '待核销'
		} else if (num == 2) {
			return '待发货'
		} else if (num == 3) {
			return '待收货'
		} else if (num == 4) {
			return '已取消'
		} else if (num == 5) {
			return '已完成'
		}
	}
	async function init(str) {
		if (str == '搜索') {
			query.status = 0
			query.page = 1
			list.value = []
		}
		const res = await orderlist(query)
		if (res.data.code == 1) {
			list.value.push(...res.data.data.list)
			console.log(list.value, 'list.valuelist.valuelist.valuelist.value');
			lastpage.value = res.data.data.lastpage
		}
	}
</script>

<style lang="scss">
	.closeicon {
		width: 40rpx;
		height: 40rpx;
		margin-left: 80rpx;
	}

	::v-deep .u-popup__content {
		background-color: rgba(255, 0, 0, 0) !important;
	}

	.popucenter {
		width: 592rpx;
		background: linear-gradient(180deg, #E8FFEA 0%, #FFFFFF 63%);
		border-radius: 28rpx 28rpx 28rpx 28rpx;
		overflow: hidden;
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;

		.close {
			width: 48rpx;
			height: 48rpx;
			position: absolute;
			top: 0%;
			right: 0%;
			margin-right: 24rpx;
			margin-top: 24rpx;
		}

		.title {
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #1D2129;
			text-align: center;
			margin-top: 48rpx;
			width: 100%;
			margin-bottom: 50rpx;
		}

		.btnss {
			width: 256rpx;
			height: 56rpx;
			background: #7EBC79;
			border-radius: 8rpx 8rpx 8rpx 8rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 28rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 56rpx;
			margin-bottom: 44rpx;
		}

		.erweima {
			width: 332rpx;
			height: 332rpx;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			margin: 30rpx auto;

		}
	}

	.msgicon {
		width: 532rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #4E5969;
			text-align: center;
			width: 100%;
			margin-top: -30rpx;
		}

		image {
			width: 532rpx;
		}
	}

	page {
		background: #F7F8FA;
	}

	.activecolor {
		color: #7EBC79 !important;
	}

	.yajinrecord {
		width: 100%;
		position: relative;
		overflow: hidden;

		.itemboxss {
			width: 686rpx;
			margin: auto;
			overflow: hidden;
			margin-top: 30rpx;

			.itembox {
				width: 686rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.18);
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-bottom: 28rpx;

				&_center {
					width: 630rpx;
					margin: auto;
					overflow: hidden;

					.bottomline {
						width: 100%;
						overflow: hidden;
						height: 124rpx;
						border-top: 1rpx solid #C9CDD4;
						display: flex;
						align-items: center;
						margin-top: 28rpx;
						flex-direction: row-reverse;

						.borderbtn {
							width: 190rpx;
							height: 64rpx;
							border-radius: 8rpx 8rpx 8rpx 8rpx;
							border: 2rpx solid #7EBC79;
							text-align: center;
							line-height: 64rpx;
							font-weight: 400;
							font-size: 28rpx;
							color: #7EBC79;
						}

						.btn {
							height: 64rpx;
							background: rgba(126, 188, 121, 0.2);
							border-radius: 8rpx 8rpx 8rpx 8rpx;
							text-align: center;
							line-height: 64rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: bold;
							font-size: 28rpx;
							color: #7EBC79;
							margin-left: 440rpx;
							padding: 0 20rpx !important;
						}
					}

					.spinfo {
						width: 100%;
						overflow: hidden;
						margin-top: 36rpx;
						display: flex;
						align-items: center;

						image {
							width: 162rpx;
							height: 144rpx;
							border-radius: 20rpx 20rpx 20rpx 20rpx;
						}

						.text1 {
							width: 452rpx;
							margin-left: 16rpx;
							overflow: hidden;

							.l1 {
								display: flex;
								align-items: center;
								font-family: PingFang SC, PingFang SC;
								font-weight: 500;
								font-size: 28rpx;
								color: #1D2129;
								display: -webkit-box;
								-webkit-box-orient: vertical;
								-webkit-line-clamp: 1;
								overflow: hidden;

								.box {
									width: 48rpx;
									height: 32rpx;
									background: #7EBC79;
									border-radius: 4rpx 16rpx 4rpx 16rpx;
									font-family: PingFang SC, PingFang SC;
									font-weight: 500;
									font-size: 24rpx;
									color: #FFFFFF;
									text-align: center;
									line-height: 32rpx;
									margin-right: 8rpx;
								}
							}

							.l2 {
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #7F7F7F;
								overflow: hidden;
								word-break: break-all;
								white-space: nowrap;
								margin-top: 18rpx;
								width: 408rpx;
							}

							.yajinbox {
								display: flex;
								align-items: center;
								margin-top: 28rpx;
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #86909C;
								justify-content: space-between;

								.tihuofs {}
							}
						}
					}

					.topline {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-top: 28rpx;

						.dingdanbinhao {
							width: 80%;
							word-break: break-all;
							/* 或 */
							display: flex;
							align-items: center;
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 28rpx;
							color: #1D2129;

							&_icon {
								width: 24rpx;
								height: 27rpx;
								margin-right: 12rpx;
							}
						}

						.statustext {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 32rpx;
							color: #FF7613;
						}
					}


				}
			}
		}

		.yajinrecordback {
			width: 100%;
			height: 530rpx;
			background: linear-gradient(180deg, #84BD78 0%, rgba(132, 189, 120, 0) 100%);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			position: absolute;
			z-index: -1;
		}

		.classifybox {
			width: 100%;
			margin: auto;
			margin-left: 32rpx;
			white-space: nowrap;
			margin-top: 40rpx;

			.itembox {
				width: 96rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 32rpx;
				color: #1D2129;
				position: relative;
				margin-bottom: 28rpx;
				display: flex;
				flex-direction: column;
				align-items: center;

				.activebox {
					width: 48rpx;
					height: 6rpx;
					background: #7EBC79;
					border-radius: 4rpx 4rpx 4rpx 4rpx;
					position: absolute;
					bottom: -70%;
					margin-top: 10rpx;
				}
			}
		}

		.seachbox {
			width: 686rpx;
			margin: auto;
			margin-top: 186rpx;
			height: 68rpx;
			background: rgba(255, 255, 255, 0.9);
			border-radius: 34rpx 34rpx 34rpx 34rpx;
			display: flex;
			align-items: center;
			position: relative;

			.lefticon {
				margin-left: 20rpx;
				width: 32rpx;
				height: 32rpx;
			}

			input {
				margin-left: 16rpx;
			}

			.seachboxbtn {
				width: 120rpx;
				height: 68rpx;
				background: #7EBC79;
				border-radius: 34rpx 34rpx 34rpx 34rpx;
				text-align: center;
				line-height: 68rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				position: absolute;
				color: #FFFFFF;
				right: 0%;
			}
		}

	}
</style>